package com.game.ball;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 游戏面板类。
 * <p>
 * 该类负责管理游戏的主要逻辑和绘图。
 */
public class BallPanel extends JPanel implements Runnable {
    private final Ball ball;
    private final Paddle paddle;
    private final List<Brick> bricks;
    private Thread gameThread;
    private boolean isRunning;

    /**
     * 构造一个新的游戏面板对象。
     */
    public BallPanel() {
        setPreferredSize(new Dimension(Constants.WIDTH, Constants.HEIGHT));
        setBackground(Color.BLACK);

        ball = new Ball(0, 0, Constants.BALL_SIZE);
        paddle = new Paddle(Constants.WIDTH / 2 - Constants.PADDLE_WIDTH / 2, Constants.HEIGHT - 30, Constants.PADDLE_WIDTH, Constants.PADDLE_HEIGHT);

        bricks = new ArrayList<>();
        for (int i = 0; i < Constants.BRICK_ROWS; i++) {
            for (int j = 0; j < Constants.BRICK_COLUMNS; j++) {
                int x = j * (Constants.BRICK_WIDTH + 5) + 25;
                int y = i * (Constants.BRICK_HEIGHT + 5) + 30;
                bricks.add(new Brick(x, y, Constants.BRICK_WIDTH, Constants.BRICK_HEIGHT));
            }
        }

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("mouuse moved");
                paddle.move(e.getX());
            }
        });

//        addMouseListener(); // 监听鼠标
        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
               // System.out.println("1111111111mouuse moved");
                paddle.move(e.getX());
            }
        }); // 监听鼠标移动

        // 添加键盘监听器
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {

                int keyCode = e.getKeyCode();
                System.out.println("KeyEvent "+keyCode);
                switch (keyCode) {
                    case KeyEvent.VK_LEFT:


                        break;
                    case KeyEvent.VK_RIGHT:


                        break;
                    case KeyEvent.VK_UP:

                        break;
                    case KeyEvent.VK_DOWN:

                        break;
                    case KeyEvent.VK_SPACE:
                   //
                        break;
                }
            }
        });
        //setFocusable(true);

        startGame();
    }

    /**
     * 开始游戏。
     */
    private void startGame() {
        isRunning = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    /**
     * 游戏主循环。
     */
    @Override
    public void run() {
        while (isRunning) {
            ball.move();

            // 检查小球与挡板的碰撞
            if (ball.getBounds().intersects(paddle.getBounds())) {
                ball.setDy(-ball.getDy());
            }

            // 检查小球与砖块的碰撞
            for (Brick brick : bricks) {
                if (brick.isVisible() && ball.getBounds().intersects(brick.getBounds())) {
                    ball.setDy(-ball.getDy());
                    brick.setVisible(false);
                }
            }

            // 检查小球是否出界
            if (ball.getY() > Constants.HEIGHT) {
                ball.reset();
            }

            repaint();
            try {
                Thread.sleep(16); // 大约每秒60帧
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 重写 paintComponent 方法以绘制游戏元素。
     *
     * @param g 用于绘制的 Graphics 对象
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ball.draw(g);
        paddle.draw(g);
        for (Brick brick : bricks) {
            brick.draw(g);
        }
    }
}